//
//  XX_XQData_History.m
//  MacStockApp
//
//  Created by MBPR-13 on 12/12/31.
//
//

#import "XX_XQData_History.h"


#import "GBTableColumnHeader.h"
#import "GBTableColumnHeader_basicSqlite.h"
#import "GBTableColumnHeader_quote.h"
#import "GBTableColumnHeader_2Row3Row.h"
#import "GBTableColumnHeader_node.h"
#import "GBTableColumnHeader_position.h"
#import "GBTableColumnHeader_position_bottom_sumaverage.h"
#import "GBTableColumnHeader_Indicator.h"
#import "GBTableColumnHeader_signal.h"
#import "GBTableColumnHeader_ObjectOtherKey.h"
#import "GBall.h"

@implementation XX_XQData_History


//是否有xq資料
+(BOOL)isHaveXQ_Dairly_Data:(NSString *)read_write_file_code{
    
    
    
    
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    NSFileManager *fileManager = [NSFileManager defaultManager];
        
    //XQ報價來源檔案存放路徑
    NSString *xq_path=[prefs objectForKey:Defaults_Path_XQ_Dairly];
    
    
    
    NSString *xq_FilepathnName= [xq_path stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.txt",read_write_file_code ]]  ;
    
    //NSLog(@"xq_FilepathnName=%@",xq_FilepathnName);
    
    BOOL isHaveXQ_Dairly_file=[fileManager fileExistsAtPath:xq_FilepathnName];
    
    
    return isHaveXQ_Dairly_file;
}
//讀取日資料
+(NSMutableDictionary *)read_XQ_Dairly_Data:(NSString *)read_write_file_code _code:(NSString *)code _timezone:(NSString *)timezone{
   
    //設定XQ 資料開始日================================================================
    NSString *start_datestring=@"2003-01-02";
    NSDate *start_date;
    {
        NSDateFormatter *df = [[NSDateFormatter alloc] init];
        [df setDateFormat:@"yyyy-MM-dd"];
        start_date = [df dateFromString: start_datestring];
        [df release];
    }     
    
    //XQ報價來源檔案存放路徑
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
   
    
    NSString *xq_path=[prefs objectForKey:Defaults_Path_XQ_Dairly];
    
    NSString *xq_FilepathnName= [xq_path stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.txt",read_write_file_code ]]  ;
    
    
    NSString *xq_data=[NSString stringWithContentsOfFile:xq_FilepathnName encoding:NSUTF8StringEncoding error:nil];
    
    NSArray *data_array=[xq_data  componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];
    
  
    BOOL issetHighLow;
    issetHighLow=NO;
    
    NSMutableArray *_his_data; //歷史資料
    _his_data= [[NSMutableArray alloc] init]  ;
    
    double his_data_high; // 最大值
    double his_data_low;  // 最小值
    his_data_low=0;
    his_data_high=0;
    
    NSDate *last_his_date=[NSDate dateWithTimeIntervalSince1970:9999];
    NSNumber *last_his_close=[NSNumber numberWithFloat: 0 ];
    
    
    //逐筆取出資料
    for (int i=1; i<[data_array count] ;i++ ) {
        NSString *line=[data_array objectAtIndex:i ];
        
        if (![line isEqualToString:@""]) {
            
            NSArray *line_array;
            
            //空白鍵分割
            line_array =  [line componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
            
            if ([line_array count]>=6) {
                
                NSMutableDictionary *dic;
                dic=[[NSMutableDictionary alloc] init];
                
                NSDate   *dates;
                {
                    
                    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
                    [dateFormatter setDateFormat:@"MM/dd/yyyy"];
                    [dateFormatter setTimeZone: [NSTimeZone   timeZoneWithName:timezone]];
                    dates = [dateFormatter dateFromString:[line_array objectAtIndex:1] ];
                    
                    [dateFormatter release];
                    
                    
                }
                
                
                double d_open=[[line_array objectAtIndex:2] doubleValue];
                double d_high=[[line_array objectAtIndex:3] doubleValue];
                double d_low=[[line_array objectAtIndex:4] doubleValue];
                double d_close=[[line_array objectAtIndex:5] doubleValue];
                double d_adj_close =[[line_array objectAtIndex:5] doubleValue];
                
                int i_volume=[[line_array objectAtIndex:6] intValue];
                
                
                if (d_low==0) {
                    d_low=d_close;
                }
                if (d_high==0) {
                    d_high=d_close;
                }
                if (d_open==0) {
                    d_open=d_close;
                }
                
                NSNumber *close=[NSNumber numberWithDouble:d_close];
                NSNumber *open=[NSNumber numberWithDouble:d_open];
                NSNumber *high=[NSNumber numberWithDouble:d_high];
                NSNumber *low=[NSNumber numberWithDouble:d_low];
                NSNumber *volume=[NSNumber numberWithInt:i_volume];
                NSNumber *adjclose=[NSNumber numberWithDouble:d_adj_close];
                
                //比較取出歷史最大值與最小值
                {
                    if (issetHighLow==NO) {
                        //第一次設定
                        his_data_low=d_close;
                        his_data_high=d_close;
                        issetHighLow=YES;
                    }
                    
                    if (d_high>his_data_high) {
                        his_data_high=d_high;
                    }
                    
                    if (d_low<his_data_low  ) {
                        his_data_low=d_low;
                    }
                    
                    
                    
                    last_his_date=dates;
                    last_his_close=adjclose; //注意此點
                    
                    
                }
                
                
                
                
                [dic setObject:dates forKey: HIS_basic_date ];
                [dic setObject:close forKey:HIS_basic_close];
                [dic setObject:open forKey:HIS_basic_open];
                [dic setObject:high forKey:HIS_basic_high];
                [dic setObject:low forKey:HIS_basic_low];
                [dic setObject:volume forKey:HIS_basic_vol];
                [dic setObject:adjclose forKey:HIS_basic_adjclose];
                
                
                
                //
                if ([dates compare:start_date]==NSOrderedDescending ){                     [_his_data addObject:dic];
                }
                
                [dic release];
                
                
            }
            
            
        }
        
        
    }
    
    //將歷史資料檔案陣列 最大值 最小值 存放在Dictionary，用來傳出去
    
    NSMutableDictionary *out_dic=[[[NSMutableDictionary alloc] init] autorelease];
    
    [out_dic setObject:code forKey:DB_object_sqlite_basic_code];
    [out_dic setObject:_his_data forKey:@"his_data"];
    [_his_data release];
    [out_dic setObject:[NSNumber numberWithDouble: his_data_high] forKey:@"his_data_high"];
    
    [out_dic setObject:[NSNumber numberWithDouble: his_data_low] forKey:@"his_data_low"];
    [out_dic setObject:last_his_date forKey:@"last_his_date"];
    [out_dic setObject:last_his_close forKey:@"last_his_close"];
    
    return out_dic;
    /*
    outarray_his_data=out_dic;
    
    outarray_his_array=[outarray_his_data objectForKey:@"his_data"];
    */
}
@end
